﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CustomControlDemo01
{
    /// <summary>
    /// ColorPickerUC.xaml 的交互逻辑
    /// </summary>
    public partial class ColorPickerUC : UserControl
    {
        public ColorPickerUC()
        {
            InitializeComponent();
        }

        public Color MyColor
        {
            get { return (Color)GetValue(MyColorProperty); }
            set { SetValue(MyColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyColorProperty =
            DependencyProperty.Register("MyColor", typeof(Color), typeof(ColorPickerUC), new PropertyMetadata(Colors.Red, OnMyColorChanged));

        private static void OnMyColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerUC colorPickerUC = d as ColorPickerUC;
            Color oldColor = (Color)e.OldValue;
            Color newColor = (Color)e.NewValue;

            colorPickerUC.RedColor = (byte)newColor.R;
            colorPickerUC.GreenColor = (byte)newColor.G;
            colorPickerUC.BlueColor = (byte)newColor.B;
        }

        public byte RedColor
        {
            get { return (byte)GetValue(RedColorProperty); }
            set { SetValue(RedColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RedColorProperty =
            DependencyProperty.Register("RedColor", typeof(byte), typeof(ColorPickerUC), new PropertyMetadata(OnRGBColorChanged));
        

        public byte GreenColor
        {
            get { return (byte)GetValue(GreenColorProperty); }
            set { SetValue(GreenColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GreenColorProperty =
            DependencyProperty.Register("GreenColor", typeof(byte), typeof(ColorPickerUC), new PropertyMetadata(OnRGBColorChanged));
        

        public byte BlueColor
        {
            get { return (byte)GetValue(BlueColorProperty); }
            set { SetValue(BlueColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BlueColorProperty =
            DependencyProperty.Register("BlueColor", typeof(byte), typeof(ColorPickerUC), new PropertyMetadata(OnRGBColorChanged));

        private static void OnRGBColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ColorPickerUC colorPickerUC = d as ColorPickerUC;
            Color oldColor = colorPickerUC.MyColor;
            if (e.Property == RedColorProperty)
            {
                oldColor.R = (byte)e.NewValue;
            }
            else if (e.Property == GreenColorProperty)
            {
                oldColor.G = (byte)e.NewValue;
            }
            else if (e.Property == BlueColorProperty)
            {
                oldColor.B = (byte)e.NewValue;
            }
            colorPickerUC.MyColor = oldColor;
        }
    }
}
